package cn.itcast.bos.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.fore.web.action.common.BaseAction;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class MenuAction extends BaseAction<Menu> {
	// 注入service
	@Autowired
	private MenuService menuService;

	// 查询菜单所有数据
	@Action("menu_list")
	public String list() {
		// 用list接收查询数据
		List<Menu> menuList = menuService.findByMenuList();
		// 压入root栈顶
		ActionContext.getContext().getValueStack().push(menuList);
		return JSON;

	}

	@Action(value = "menu_add", results = { @Result(type = "redirect", location = "/pages/system/menu.html") })
	public String add() {
		menuService.saveMenu(model);
		return SUCCESS;

	}

	@Action("menu_listByUser")
	public String listByUser() {
		// 获取shiro中的当前登录的用户对象(任意类中)
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		// 调用业务层
		List<Menu> menuList = menuService.findByMenuListByUser(user);
		// 压栈顶
		ActionContext.getContext().getValueStack().push(menuList);
		return JSON;

	}
}
